package cn.edu.xhu.library_management.department.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.department.entity.LmFaculty;
import cn.edu.xhu.library_management.department.entity.LmMajor;
import cn.edu.xhu.library_management.department.entity.search.SearchMajor;
import cn.edu.xhu.library_management.department.entity.vo.MajorVo;
import cn.edu.xhu.library_management.department.service.FacultyService;
import cn.edu.xhu.library_management.department.service.MajorService;
import cn.edu.xhu.library_management.common.validator.ValidationGroups;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-01-29
 * @Description: 专业Controller
 */
@Api(tags = "专业模块")
@ApiSort(2)
@RestController
@RequestMapping("/lm_department")
public class MajorController {
    @Autowired
    private MajorService majorService;
    @Autowired
    private FacultyService facultyService;

    // 查找所有专业
    @ApiOperation(value = "查找所有专业",produces = "application/json")
    @ApiOperationSupport(order = 211)
    @GetMapping("/major")
    public R findAllMajor(){
        List<LmMajor> majors = majorService.findAllMajor();
        List<MajorVo> majorVos = convertVoList(majors);
        return !majorVos.isEmpty()?R.ok().data("majors",majorVos):R.error("数据为空!!!");
    }


    // 通过院系ID查找对应的专业
    @ApiOperation(value = "通过院系ID查找对应的专业",produces = "application/json")
    @ApiOperationSupport(order = 212)
    @GetMapping("/major/faculty/{faId}")
    public R findMajorsByFaculty(@PathVariable("faId") Integer faId){
        List<LmMajor> majors = majorService.findMajorsByDepartment(faId);
        List<MajorVo> majorVos = convertVoList(majors);
        return !majorVos.isEmpty()?R.ok().data("majors",majorVos):R.error("数据为空!!!");
    }

    // 通过专业ID查找专业
    @ApiOperation(value = "通过专业ID查找专业",produces = "application/json")
    @ApiOperationSupport(order = 213)
    @GetMapping("/major/{maId}")
    public R findMajorById(@PathVariable("maId") Integer maId){
        LmMajor major = majorService.findMajorById(maId);
        return major != null ? R.ok().data("major",major):R.error("查无此数据!!!");
    }

    // 搜索专业(带分页)
    @ApiOperation(value = "搜索专业(带分页)",produces = "application/json")
    @ApiOperationSupport(order = 214)
    @GetMapping("/major/search")
    public R search(SearchMajor searchMajor){
        Map<String, Object> map = majorService.search(searchMajor);
        List<MajorVo> majorVos = convertVoList((List<LmMajor>) map.get("majors"));
        map.put("majors",majorVos);
        return R.ok().data(map);
    }

    // 添加专业
    @ApiOperation(value = "添加专业",produces = "application/json")
    @ApiOperationSupport(order = 221)
    @PostMapping("/major")
    public R addMajor(@Validated @RequestBody LmMajor major){
        return majorService.addMajor(major) ? R.ok("添加专业成功!!!"):R.error("添加专业失败!!!");
    }

    // 修改专业(专业ID)
    @ApiOperation(value = "修改专业(专业ID)",produces = "application/json")
    @ApiOperationSupport(order = 231)
    @PutMapping("/major")
    public R updateMajor(@Validated(ValidationGroups.LmMajorUpdate.class) @RequestBody LmMajor major){
        return majorService.updateMajor(major)?R.ok("修改专业成功!!!"):R.error("修改专业失败!!!");
    }

    // 删除专业(专业ID)
    @ApiOperation(value = "删除专业(专业ID)",produces = "application/json")
    @ApiOperationSupport(order = 241)
    @DeleteMapping("/major/{maId}")
    public R deleteMajor(@PathVariable("maId") Integer maId){
        return majorService.deleteMajorById(maId)?R.ok("删除专业成功!!!"):R.error("删除专业失败!!!");
    }

    // 删除专业(批量删除)
    @ApiOperation(value = "删除专业(批量删除)",produces = "application/json")
    @ApiOperationSupport(order = 242)
    @DeleteMapping("/major")
    public R deleteMajors(@RequestParam("maIds") List<Integer> maIds){
//        return majorService.deleteMajor(maId)?R.ok("删除专业成功!!!"):R.error("删除专业失败!!!");
        return majorService.deleteMajorByIds(maIds)?R.ok("批量删除专业成功!!!"):R.error("批量删除专业失败!!!");
    }


    /**
     * 将major实体类，封装成majorVo显示类
     * @param majors major实体对象集合
     * @return majorVo显示对象集合
     */
    private List<MajorVo> convertVoList(List<LmMajor>  majors) {
        List<MajorVo> majorVos = new ArrayList<>();
        if (!majors.isEmpty()){
            // 不为空直接转换
            majors.forEach(major -> {
                MajorVo vo = new MajorVo();
                // 查询院系对应id下对应的名称
                LmFaculty faculty = facultyService.findFacultyById(major.getFaId());
                BeanUtils.copyProperties(major,vo);
                vo.setFacultyName(faculty.getName());
                majorVos.add(vo);
            });
            return majorVos;
        }
        return null;
    }
}
